package com.ruoyi.radar.cal;

import Jama.Matrix;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.stereotype.Component;

import java.util.LinkedList;

@Component
public class KeGridData {

    public double[][] gridData(double[] x, double[] y, double[] z,
                                       double[][] X, double[][] Y){
        int xLength = x.length;
        int xSize = X.length;
        int ySize = X[0].length;
        double[][] data = new double[xLength + 1][xLength + 1];
        double[][] data2 = new double[xSize][ySize];

        for (int i = 0; i <= xLength; i++) {
            for (int j = 0; j <= xLength; j++) {
                if ((i < xLength) && (j < xLength)) {
                    if (i != j) {
                        data[i][j] = Math.sqrt((x[i] - x[j]) * (x[i] - x[j])
                                + (y[i] - y[j]) * (y[i] - y[j]));
                    } else {
                        data[i][j] = 0.0001;
                    }
                } else if ((i == xLength) || (j == xLength)) {
                    data[i][j] = 1;
                }
                if (i == xLength && j == xLength) {
                    data[i][j] = 0;
                }
            }
        }
        //矩阵的初始化
        Matrix m = new Matrix(data);
        //矩阵转秩
        Matrix mMatrix = m.inverse();
        for (int i = 0; i < xSize; i++) {
            for (int j = 0; j < ySize; j++) {
                double[] rData = new double[xLength + 1];
                for (int k = 0; k <= xLength; k++) {
                    if (k < xLength) {
                        rData[k] = Math.sqrt((X[i][j] - x[k])
                                * (X[i][j] - x[k]) + (Y[i][j] - y[k])*
                                (Y[i][j] - y[k]));
                    } else {
                        rData[k] = 1;
                    }
                }
                double[] rDa = new double[xLength + 1];
                for (int k = 0; k < (xLength + 1); k++) {
                    for (int k2 = 0; k2 < (xLength + 1); k2++) {
                        rDa[k] += mMatrix.get(k, k2) * rData[k2];
                    }
                }
                for (int k = 0; k < xLength; k++) {
                    data2[i][j] += z[k] * rDa[k];
                }
            }
        }
        return data2;
    }

}
